import logging
import logging.handlers
import os

LOGGERS = {}

def CreateFolder(fullPathname):
    existed = os.path.exists(fullPathname)
    if not existed:
        os.makedirs(fullPathname)
        print("Create Folder: " + fullPathname)
    else:
        #print("Folder Existed: " + fullPathname)
        pass


def InitLogger(loggerName):
    pass


#
def GetLogger(loggerName, consoleOutput=True, logDir=""):
    #
    if loggerName not in LOGGERS:
        #
        logger = logging.getLogger(loggerName)
        logger.setLevel(logging.INFO)  # Log等级总开关

        # ---File Name---
        if logDir == "":
            logDir = "D:\\logs\\"
        folder = logDir + "\\" + loggerName
        CreateFolder(folder)

        # fh = logging.FileHandler(folder + "\\" + loggerName , mode='a')
        fh = logging.handlers.TimedRotatingFileHandler(folder + "\\" + loggerName, when='D', interval=1, encoding="utf-8")

        # 指定logger输出格式
        formatter = logging.Formatter(fmt='%(asctime)s %(levelname)-8s: %(message)s')
        fh.setFormatter(formatter)
        logger.addHandler(fh)

        # 往屏幕上输出
        if consoleOutput:
            sh = logging.StreamHandler()  # 往屏幕上输出
            sh.setFormatter(formatter)  # 设置屏幕上显示的格式
            logger.addHandler(sh)

        #
        LOGGERS[loggerName] = logger

    return LOGGERS[loggerName]

